<template>
<div class="content">
  <el-dialog title="新增用戶" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
    <el-form   label-width="100px"  ref="addStationUploadForm" class="imgUploadForm" style="position: absolute;top: 400px;left:200px">
      <!-- 代码中的1表示第几个 和引入js的type变量对应 -->
      <!-- action 就是原来普通form action也就是提交地址 所以对应方法返回后端正确的action请求地址-->
      <el-form-item label="站点图片:"  style="margin-left: 0px;">
        <el-upload
          class="upload"
          :action="getUploadUrl"
          list-type="picture-card"
          :limit="limitNum"
          accept="image/*"
          :on-change="(file,fileList)=>{imgHandleChange(file,fileList,1,this,'addStationUploadForm','station')}"
          :on-remove="(file,fileList)=>{imgHandleRemove(1,this,file)}"
          :on-before="(file,fileList)=>{imghandleBeforeUpload(file,1,this)}"
          :auto-upload="false"
          :file-list="fileList"
          ref="file1">
          <i class="el-icon-plus"></i>
        </el-upload>
        <!--<viewer-dialog ref="viewer" :images="imagesUp"></viewer-dialog>-->
      </el-form-item>
    </el-form>
  <el-form :model="station" :rules="addRules" ref="addFrom" label-width="80px">
    <el-col :span="6">&nbsp</el-col>
    <el-form-item label="站点名称"  label-width="100px">
      <el-col :span="8">
      <el-input v-model="station.stationname" ></el-input>
      </el-col>
    </el-form-item>
    <el-col :span="6">&nbsp</el-col>
    <el-form-item label="管理员名字" label-width="100px">
      <el-col :span="8">
      <el-input v-model="station.stationmanngernmae"></el-input>
      </el-col>
    </el-form-item>
    <el-col :span="6">&nbsp</el-col>
    <el-form-item label="密码" label-width="100px" >
      <el-col :span="8">
      <el-input v-model="station.stationpass"></el-input>
      </el-col>
    </el-form-item>
    <el-col :span="6">&nbsp</el-col>
    <el-form-item label="站点号" label-width="100px">
      <el-col :span="8">
      <el-input v-model="station.stationnum"></el-input>
      </el-col>
    </el-form-item>
    <el-col :span="6">&nbsp</el-col>
    <el-form-item label="电话号码" label-width="100px">
      <el-col :span="8">
    <el-input v-model="station.stationmanngertel"></el-input>
      </el-col>
  </el-form-item>
    <div style="width:200px;height:200px;">

    </div>
    <el-form-item>
      <el-col :span="8">
      <el-button type="primary" @click="onSubmit">提交</el-button>
      <el-button>取消</el-button>
      </el-col>
    </el-form-item>
  </el-form>
  </el-dialog>
</div>
</template>

<script>
import api from "../../../../axios/api.js"
import rules from "../../../../static/js/validator/rules.js"
import utils from "../../../../static/js/utils/upload.js"
import $ from 'jquery'
export default {
  props:["addDialogIsShow"],
  name: "add",
  data(){
    return {
      station:{
        stationimage:""
      },
      addRules: {
        // adname: [
        //   {required: true, message: "姓名不能为空", trigger: 'blur'},
        // ],
        // adnickname: [
        //   {required: true, message: "别名不能为空", trigger: 'blur'},
        // ],
        // adpassword: [{required: true, message: "密码不能为空", trigger: 'blur'}, {
        //   validator:rules.FormValidate.Form().validatePsdReg,trigger:'change'
        // }],
        // repassword: [{required: true, message: "密码不能为空", trigger: 'blur'}, {
        //   validator:rules.FormValidate.Form().validatePsdReg,trigger:'change'
        // }],
        // adphone: [{required: true, message: "电话不能为空", trigger: 'blur'}, {
        //   validator:rules.FormValidate.Form().validatePhone,trigger:'change'
        // }],
        // email: [{required: true, message: "电子邮箱不能为空", trigger: 'blur'}, {
        //   validator:rules.FormValidate.Form().validateEmail,trigger:'change'
        // }]
      },
      //校验结束
      //上传使用
      fileList: [], //缓存区文件
      uploadFile:[], //  上传用文件
      formData:{files:[]},
      imagesUp:null,
      //上传使用结束
      test:true,
      limitNum:6
    }
  },
  methods:{
    onSubmit:function() {
      const _this = this;
      this.$refs['addFrom'].validate(valid => {//此处refs对象和标签上的ref对应
        console.log(this.station);
        if (valid) {
          api.setDataFromAxios("/api/stationController/addStation", this.station, "post", null).then(
            dto => {
              this.$message.success(dto.msg);
              _this.$emit("closeAddDialog");
              _this.$emit("getStationByPage");
            }
          )
        } else {
          this.$message.warning("请正确格式填写后提交");
          return
        }
      })
    },
    handleClose:function(){
      //给父组件传参
      this.$emit("closeAddDialog");
    },

    //多文件上传
    imghandleBeforeUpload(file,type,vm) {
      return utils.UPLOAD.Upload().handleBeforeUpload(file,type,vm);
    },
    imgHandleChange(file, fileList, type,vm,modelName,name) {
      console.log(fileList);
      return utils.UPLOAD.Upload().handleChange(file, fileList, type,vm,modelName,name);
    },
    imgHandleRemove(type,vm,file) {

      let index =  utils.UPLOAD.Upload().handleRemove( type,vm,file);
      $(".el-upload--picture-card").css("display","inline-block");
      alert(index);
      console.log("----------777--------------");
      this.station.stationimage = utils.UPLOAD.Upload().delStringByIndex(this.station.stationimage,index);
      console.log(this.station.stationimage);

      return index>0?true:false;
    },
    uploadImg:function(modelName,name,file){
      //  使用formdata格式
      let formData = new FormData();//准备一个空的表单对象
      formData.append("model",modelName);//图片上传子目录名放入表单对象
      if (this.formData.files[name]) {
        formData.append("file", this.formData.files[name]);//文件放入表单对象
      }
      console.log("----------888--------------");
      //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
      api.setDataFromAxios("/api/uploadController/upload",formData,"post",{headers: { "Content-Type": "multipart/form-data" }})
        .then(dto=>{
          if(dto.code==200){
            this.$message.success (dto.msg);
            if (this.station.stationimage==''){
              this.station.stationimage = dto.tList[0];//第一次增加特殊处理
            }else {
              this.station.stationimage = this.station.stationimage + "," + dto.tList[0];
            }
            this.fileList.push(file);
            if (this.fileList.length==this.limitNum){
              $(".el-upload--picture-card").css("display","none");
            }
            console.log(this.station.stationimage);
          }else{
            this.$message.error (dto.msg);
          }
        });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
    },
    //多文件上传结束
  },
  computed:{
    getUploadUrl(){
      return      "/api/uploadController/upload";
    }
  }
}
</script>

<style scoped lang="stylus">
.content
  position relative
/deep/ .el-form-item__content
  margin-left 0px;
</style>
